package com.zxf.appliction.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zxf.appliction.domain.Columns;
import com.zxf.appliction.domain.Params;
import com.zxf.appliction.domain.TreeBean;
import com.zxf.appliction.service.ColumnService;

/**
 * 菜单管理
 * ColumnController
 * @author 郑肖峰
 * @since  2017-5-23 下午6:05:03
 */
@RequestMapping("/column")  
@Controller
public class ColumnController {

	@Autowired 
	private ColumnService columnService;
	private static Logger logger = Logger.getLogger(ColumnController.class);
	
	
	/**
	 * 保存菜单信息
	 * @param columns
	 * @param firthcombo
	 * @param erji
	 * @param sanji
	 * @param fourth
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/addcolumns", method = RequestMethod.POST )  
    public @ResponseBody Map<String,Object> saveOrUpdate(Columns columns,String firthcombo,String erji,String sanji,String fourth, HttpServletRequest request,     
            HttpServletResponse response){  
        Map<String,Object> responseMap = new HashMap<String,Object>();  
  
  //判断添加的子菜单的父菜单
                int parentid =0;  
                String temparentid ="";
                
                if(firthcombo !=null && firthcombo !=""){
                	temparentid = firthcombo;
                }
                if(erji !=null && erji !=""){
                	temparentid= erji;
                }
                if(sanji !=null && sanji !=""){
                	temparentid= sanji;
                }
                if(fourth !=null && fourth !=""){
                	 temparentid= fourth;
                }
                
               
                if(temparentid!=null && temparentid != "" ){
                	parentid=Integer.parseInt(temparentid);
                	
                }
                if(columns.getLeaf() != null && columns.getLeaf() !=""){
                	columns.setType("URL");
                }
                columns.setParentid(parentid);
                columns.setRootid(Integer.parseInt(firthcombo));
                columnService.insertUser(columns);  
                responseMap.put("method", "Create");  
                responseMap.put("success", "true");  
                responseMap.put("info", "新增成功！"); 
                return responseMap; 
            }  
          //节点json数据   
		@RequestMapping(value="/columnlist", method = RequestMethod.POST)
		@ResponseBody
		public  JSONArray columnlist(String parentID,String node, HttpServletRequest request, HttpServletResponse response) {
			 int aa=0;
			 if(null!=parentID && ""!=parentID){
				 aa= Integer.parseInt(parentID);
				 
			 }
			 
			 List columnlist = columnService.findLeaf(aa); 
			//Map<String, Object> treelist =  (Map<String, Object>) columnService.getUserList2();
			List list = new ArrayList();
			boolean flag=false;
			for(int i=0;i<columnlist.size();i++){
				Columns column = new Columns();
				TreeBean treeBean =	new TreeBean();
				column = (Columns) columnlist.get(i);
				treeBean.setRootid(column.getRootid().toString());
				//treeBean.setText(column.getClassname());
				treeBean.setUrl(column.getColumnurl());
				//treeBean.setLeaf((boolean) column.isLeaf());
				treeBean.setParentID(column.getParentid().toString());
				treeBean.setChannelID(column.getChannelid().toString());
				list.add(treeBean);
			}
			 JSONArray jsonArr = JSONArray.fromObject(columnlist);
			//	obj = JSONObject.fromObject(columnlist);
			              logger.info(jsonArr.toString());
				return jsonArr;
			
		}

		
	   
		/**
		 * 所有菜单列表 
		 * @param params
		 * @param parentID
		 * @param request
		 * @param response
		 * @return list 集合
		 */
			@RequestMapping(value="/columnAllist", method = RequestMethod.POST)
			@ResponseBody
			public  JSONObject columnAllist(Params params,String parentID, HttpServletRequest request, HttpServletResponse response) {
				JSONObject obj = new JSONObject();
				
					int start = (params.getPage() - 1) * params.getLimit();
					params.setStart(start);
				 
				 List columnlist = columnService.getColumnsList(params); 
				 int totle =columnService.getcolumntotle(params);
				
					obj.put("columnlist", columnlist);
					obj.put("total", totle); // 统计数量，这里写死10条
					obj.put("success", true);
				 
				              System.out.println(obj.toString());
				             // System.out.println("json2:"+json2);
					return obj;
				
			}
			
			/**
			 * 级联菜单
			 * @param typeID
			 * @param parentID
			 * @param parentID2
			 * @param request
			 * @param response
			 * @return
			 */
		 @RequestMapping(value="/getTreeList", method = RequestMethod.POST)
		 @ResponseBody
		    public JSONArray getTreeList(String typeID,String parentID,String parentID2,HttpServletRequest request,
		            HttpServletResponse response) {
			 JSONArray jsonArr = new JSONArray();
			 HashMap<String, Integer> paramsmap =  new HashMap<String, Integer>();
			 int aa=0,leaf =0;
			
			 if(null!=parentID && ""!=parentID && typeID==null){
				 return jsonArr;
			 }
			 if(null!=parentID && ""!=parentID){
				 aa= Integer.parseInt(parentID);
				 
			 }
			 paramsmap.put("parentID", aa);
			 paramsmap.put("leaf", leaf);
			 List columnlist = columnService.findColumnLeaf(paramsmap); 
			 jsonArr = JSONArray.fromObject(columnlist);
		     logger.info("级联菜单：" + jsonArr.toString());
		     return jsonArr;
		        
		    }
		
	
		
		 /**
		  * 删除菜单
		  * @param channelID
		  * @param leaf
		  * @param jsonData
		  * @param request
		  * @param response
		  */
			@ResponseBody
			@RequestMapping(value = "/delColumn.do", method = RequestMethod.POST)
			public void delColumn(String channelID, String leaf, String jsonData,
					HttpServletRequest request, HttpServletResponse response) {
				int flagadd = 0;
				List<Integer> channelIDList = null;
				if(channelID!=null && channelID !=""){
					 channelIDList =  columnService.getchannelIDs(Integer.parseInt(channelID));	
				}
				Map<String, Object> responseMap = new HashMap<String, Object>();
			
			channelIDList.add(Integer.parseInt(channelID));
				
					flagadd = columnService.delColumn(channelIDList);
					
				
				if (flagadd > 0) {
					responseMap.put("method", "del");
					responseMap.put("success", "true");
					responseMap.put("info", "删除成功！");

					response.setContentType("text/html;charset=UTF-8");  
					try {
						response.getWriter().write("{success:true}");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} 
			
			// 获取菜单信息
			@RequestMapping(value="/getcolumn", method = RequestMethod.POST )  
		    public @ResponseBody JSONObject getcolumn(String channelID, HttpServletRequest request,     
		            HttpServletResponse response){  
		    	JSONObject obj = new JSONObject();
		   
		        Columns column = new Columns();
		                if(channelID!=null && channelID !=""){
		                	column = columnService.getColumnbyid(Integer.parseInt(channelID));
		                	
		                }
		                obj.put("data", column);
		        		obj.put("success", true);
		                return obj; 
		            }  
}
